import re

from common.BasePages import BasePage


class ProjectPage(BasePage):
    def project_url(self):
        self.goto_url("projectConfig")

    def create_new_project(self):
        # 创建项目向导
        self.page.locator("button[class='el-button btnClass el-button--primary el-button--medium is-plain']").click()
        self.page.locator("//input[contains(@oninput, '=')]").fill("张院龙项目")
        self.page.locator("div[class='ml10 el-input el-input--medium el-input--suffix']").click()
        self.page.locator(".BMap_mask").click()
        self.page.locator(".BMap_mask").click()
        self.page.get_by_role("button", name="确 定").click()
        self.page.get_by_role("button", name="下一步").click()
        self.page.locator("input[placeholder='请输入网关别名']").fill("网关名称")
        self.page.locator("input[placeholder='请输入网关SN']").fill("30100B14165BCC32")
        self.page.locator("input[placeholder='请输入网关别名']").click()
        self.page.get_by_text("添加串口").click()
        self.page.get_by_role("button", name="下一步").click()

        self.page.locator("//*[text() = ' 新增']").click()
        self.page.locator("input[placeholder='请输入点位名称，40字内']").fill("Bool")
        self.page.locator("input[placeholder='10进制数']").fill("1")
        self.page.get_by_role("textbox", name="请选择权限").click()
        self.page.get_by_role("listitem").filter(has_text="R/W").click()
        self.page.get_by_role("button", name="确 定").click()

        self.page.locator("//*[text() = ' 新增']").click()
        self.page.locator("input[placeholder='请输入点位名称，40字内']").fill("Int16 01")
        self.page.get_by_role("textbox", name="请选择寄存器地址区").click()
        self.page.get_by_role("listitem").filter(has_text="4X").click()
        self.page.get_by_role("textbox", name="前10进制数.后16进制数").click()
        self.page.get_by_role("textbox", name="前10进制数.后16进制数").fill("22")
        self.page.get_by_role("textbox", name="请选择权限").click()
        self.page.get_by_role("listitem").filter(has_text="R/W").click()
        self.page.get_by_role("textbox", name="请选择数据类型").click()
        self.page.get_by_role("listitem").filter(has_text=re.compile(r"^Int16 AB$")).click()
        self.page.get_by_role("button", name="确 定").click()

        self.page.locator("//*[text() = ' 新增']").click()
        self.page.locator("input[placeholder='请输入点位名称，40字内']").fill("Int16 02")
        self.page.get_by_role("textbox", name="请选择寄存器地址区").click()
        self.page.get_by_role("listitem").filter(has_text="4X").click()
        self.page.get_by_role("textbox", name="前10进制数.后16进制数").click()
        self.page.get_by_role("textbox", name="前10进制数.后16进制数").fill("32")
        self.page.get_by_role("textbox", name="请选择权限").click()
        self.page.get_by_role("listitem").filter(has_text="R/W").click()
        self.page.get_by_role("textbox", name="请选择数据类型").click()
        self.page.get_by_role("listitem").filter(has_text=re.compile(r"^Int16 AB$")).click()
        self.page.get_by_role("button", name="确 定").click()

        self.page.locator("//*[text() = '结束并保存']").click()
        self.page.get_by_role("button", name="确定").click()

    def create_basis_project(self):
        # 创建基本信息
        self.page.locator("button[class='el-button btnClass el-button--primary el-button--medium is-plain']").click()
        self.page.locator("//input[contains(@oninput, '=')]").fill("张院龙")
        self.page.locator("div[class='ml10 el-input el-input--medium el-input--suffix']").click()
        self.page.locator(".BMap_mask").click()
        self.page.locator(".BMap_mask").click()
        self.page.get_by_role("button", name="确 定").click()
        self.page.locator("//*[text() = '结束并保存']").click()
        self.page.get_by_role("button", name="确定").click()

    def edit_project(self):
        self.page.get_by_role("row", name="  ", exact=True).locator("i").nth(1).click()
        self.page.get_by_role("textbox", name="请输入项目编码").click()
        self.page.get_by_role("textbox", name="请输入项目编码").fill("140640")
        self.page.get_by_role("button", name="保存").click()
        self.assert_alert_content("编辑项目基础信息成功")

    def delete_project(self):
        self.page.locator(
            ".el-table__fixed-body-wrapper > .el-table__virtual-wrapper > div > .el-table__body > tbody > tr > .el-table_1_column_3 > .cell > .basicTableBtnBox > .el-icon-delete").first.click()
        self.page.get_by_role("button", name="确认").click()
        self.assert_alert_content("删除项目成功")

    def real_time_data(self):
        # 实时数据
        self.click_more_functions("项目管理", "实时数据")

    def project_copy(self):
        # 项目复制
        self.click_more_functions("项目管理", "复制")
        self.page.get_by_role("button", name="下一步").click()
        self.page.get_by_role("cell", name="/16").get_by_role("textbox").click()
        self.page.get_by_role("cell", name="/16").get_by_role("textbox").fill("30700B14165BCC34")
        self.page.get_by_role("button", name="下一步").click()
        self.page.get_by_role("button", name="下一步").click()
        self.page.get_by_role("button", name="下一步").click()
        self.page.get_by_role("button", name="提交").click()
        self.assert_alert_content("复制成功")
